package ai.peanut.one.api.controller;

import ai.peanut.one.api.mq.mqTestTopic.TopicSend;
import ai.peanut.one.api.tread.test2.ThreadCus;
import ai.peanut.one.common.exception.TestException;
import ai.peanut.one.common.redis.RedissonUtils;
import ai.peanut.one.ActivityParam;
import ai.peanut.one.entity.Activity;
import ai.peanut.one.service.IActivityService;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class TestController {

    @Autowired
    private RedissonUtils redissonUtils;
    @Autowired
    private IActivityService iActivityService ;
    @Autowired
    private TopicSend topicSend ;
    @Autowired
    private RedissonClient redissonClient;

    @RequestMapping("/redis")
    public String redis(){
//        redissonClient.cre
        RLock lock = null ;
        try {
            lock = redissonUtils.getRLock("lockkkkk");
            lock.lock();

            RBucket<String> rBucket = redissonUtils.getRBucket("1111");
            rBucket.set("987886" , 300000, TimeUnit.MILLISECONDS);

            Activity activity = new Activity() ;
            activity.setName("111111");
            RBucket<Activity>   rBucketBean = redissonUtils.getRBucket("1111");
            rBucketBean.set(activity , 300000, TimeUnit.MILLISECONDS);

            RMap<String, String> map = redissonUtils.getRMap("anyMap");
            String a  = map.put("123", "ooooooooo");
            map = redissonUtils.getRMap("anyMap");


            RList<String> list = redissonUtils.getRList("anyList");
            list.add("list1");
            String dskj = list.get(0);
            list = redissonUtils.getRList("anyList");

        }catch (Exception e ){

        }finally {
            if(lock != null){lock.unlock();}
        }
        return "-----" ;
    }

    @RequestMapping("/a")
    public String a(){
        redissonUtils.getRBucket("1111").set("987886" , 300000, TimeUnit.MILLISECONDS);

        return "-----" ;
    }

    @RequestMapping("/mq")
    public String mq(){
        topicSend.send4();
        return "-----" ;
    }

    @RequestMapping("/mqs")
    public String mqs(){
        topicSend.sendDispearToRefund();
        return "-----" ;
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request, HttpServletResponse response, ActivityParam param) {
        Map<String, Object> map=new HashMap<String, Object>();
//        redis.sadd("aaaa","dsfdsf");
//        redis.expire("aaa",10000);
        try {
            String pageNoStr =  request.getParameter("page") ;
            String rowsStr =  request.getParameter("rows") ;
            Integer pageNo = 1 ;
            Integer rows = 20 ;
            if(StringUtils.isNotEmpty(pageNoStr )) {
                pageNo = Integer.parseInt(pageNoStr);
            }
            if(StringUtils.isNotEmpty(rowsStr )) {
                rows = Integer.parseInt(rowsStr);
            }
            Page page = new Page(pageNo,rows) ;
            page.setOrderByField( "id" ) ;
            page.setAsc( false ) ;
            System.out.print("3333333");
            page = iActivityService.selectActivity(page,new ActivityParam()) ;

            map.put("total", page.getTotal() ) ;
            map.put("rows", page.getRecords()) ;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print("3333331111111113 ");
        }
        return map ;
    }

    @GetMapping(value = "testT1")
    @ResponseBody
    public Map<String, Object> testT1(HttpServletRequest request, HttpServletResponse response, ActivityParam param) {
        Map<String, Object> map=new HashMap<String, Object>();
        try {
            iActivityService.testC();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print("3333331111111113 ");
        }
        map.put("total", "" ) ;
        map.put("rows", "") ;
        return map ;
    }

    @GetMapping(value = "testT2")
    @ResponseBody
    public Map<String, Object> testT2(HttpServletRequest request, HttpServletResponse response, ActivityParam param) {
        Map<String, Object> map=new HashMap<String, Object>();
        try {
            iActivityService.testCc();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print("3333331111111113 ");
        }
        map.put("total", "" ) ;
        map.put("rows", "") ;
        return map ;
    }

    @GetMapping(value = "testT3")
    @ResponseBody
    public Map<String, Object> testT3(HttpServletRequest request, HttpServletResponse response, ActivityParam param) {
        Map<String, Object> map=new HashMap<String, Object>();
        try {
            iActivityService.testCcc();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print("3333331111111113 ");
        }
        map.put("total", "" ) ;
        map.put("rowdds", "") ;
        return map ;
    }

    public static  void  main(String [] args ) {
        TestException e = new TestException() ;
    }
}
